<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="../js/vue.js"></script>
</head>

<body>
  <div id="app">
    <cpn></cpn>
    <cpn></cpn>
    <cpn ref="aaa"></cpn>
    <button @click="btnClick">按钮</button>
  </div>

  <template id="cpn">
    <div>我是子组件</div>
  </template>

  <script>
    var vm = new Vue({
      el: '#app',
      data: {},
      methods: {
        btnClick() {
          // 1.$children
          // console.log(this.$children);
          // // 拿到子组件,调用子组件的方法
          // this.$children[0].showMessage()
          // console.log(this.$children[0].name);

          // 2.$refs => 对象类型,默认是一个空的对象,必须在组件上加ref='xxx',里面就会有相关的属性
          console.log(this.$refs.aaa.name);
        }
      },
      components: {
        cpn: {
          template: '#cpn',
          data() {
            return {
              name: '我是子组件的name'
            }
          },
          methods: {
            showMessage() {
              console.log('子组件的showMessage');
            }
          }
        }
      }

    });
  </script>
</body>

</html>